<?php

namespace App\Http\Controllers;

use App\Model\Post;
use App\Model\Comment;
use App\Model\Like;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;

class PostController extends Controller
{
    public function index(){
    	$posts = Post::orderBy('created_at','desc')->withCount(['comments','likes'])->paginate(2);
    	return view('post/index',compact('posts'));
    }

    public function show(Post $post){
        $post->load('comments');//

        return view('post/show',compact('post'));
    }

    public function create(){
    	return view('post/create');
    }

    public function store(Request $request){
        $this->validate($request,[
            'title' => 'required|max:50|string',
            'content' => 'required|min:10|string',
            ]);
        $user_id = \Auth::id();
        $title = $request->input('title');
        $content = $request->input('content');
        $post = new post();
        $post->title = $title;
        $post->content = $content;
        $post->user_id = $user_id;
        $post->save();
        return redirect('/posts');
    }

    public function edit(Post $post){
    	return view('post/edit',compact('post'));
    }

    public function update(Request $request,Post $post){
        //验证
        $this->validate($request,[
            'title' => 'required|max:50|string',
            'content' => 'required|min:10|string'
            ]);
        //权限
        $this->authorize('update', $post);
        //逻辑
        $title = $request->input('title');
        $content = $request->input('content');
        $post->update(['title'=>$title,'content'=>$content]);//不懂
        return redirect('/posts');
    }

    public function delect(Post $post){
        $this->authorize('delect', $post);
        $post->delete();
        return reirect('/posts');
    }

    public function imageUpload(Request $request){
        $filepath = $this->getUploadFolder();
        if($request->hasFile('wangEditorH5File') && $request->file('wangEditorH5File')->isValid()){
            $logoFile = $request->file('wangEditorH5File')->store($filepath);
        }
        $img = Image::make(asset('upload/'.$logoFile))->fit(1000, 500)->insert(asset('upload/2017/08/shuiying.jpg'), 'bottom-right', 15, 10);
        $img->save(public_path().'/upload/'.$logoFile);
        return asset('upload/'.$logoFile);
    }

    //评论
    public function review(Request $request, Post $post){
        $this->validate($request,[
            'content' => 'required|min:3',
        ]);
        $comment = new Comment();
        $comment->user_id = \Auth::id();
        $comment->content = $request->input('content');
        $post->comments()->save($comment);
        return back();
    }

    public function like(Post $post){
        $param = [
            'post_id' => $post->id,
            'user_id' => \Auth::id(),
        ];

        Like::firstOrCreate($param);
        return back();
    }

    public function unLike(Post $post){
        $post->like(\Auth::id())->delete();
        return back();
    }

    public function getUploadFolder(){
        $folder = date('Y/m');
        $uploadPath = config('filesystems.disks.upload.root');

        $uploadPath = implode('/', [$uploadPath, $folder]);
        return $folder;
    }

}
